//
//  CQRefreshView.swift
//  swift-weibo
//
//  Created by 陈乾 on 15/11/3.
//  Copyright © 2015年 陈乾. All rights reserved.
//

import UIKit

class CQRefreshView: UIView {

    //箭头
    @IBOutlet weak var arrowView: UIImageView!
    //箭头的view
    @IBOutlet weak var arrowRefreshView: UIView!
    
    //圈圈
    @IBOutlet weak var rotationView: UIImageView!
    
    //从xib加载
    class func loadRefreshViewFormXib() ->CQRefreshView{
    
        return NSBundle.mainBundle().loadNibNamed("RefreshView", owner: self, options: nil)[0] as! CQRefreshView
        
    }
    //MARK: - 箭头旋转
    func rotationArrow(isUp:Bool)
    {
        
        UIView.animateWithDuration(0.25) { () -> Void in
            self.arrowView.transform = isUp ? CGAffineTransformMakeRotation(CGFloat(M_PI-0.01)) : CGAffineTransformIdentity
        }
        
    
    }
    //MARK: - 带箭头的view隐藏 开启核心动画
    func  playRotationView() {
     
         let rotationKey: String = "rotationKey"
        //如果找到相同的动画 返回 别在开启一次动画了
        if let anima = rotationView.layer.animationForKey(rotationKey) {
         return
        }
        
        //带箭头的view隐藏
        arrowRefreshView.hidden = true
        //开启核心动画
       let animation = CABasicAnimation(keyPath: "transform.rotation")
        //设置属性
        animation.toValue = M_PI*2
        animation.duration = 0.8
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        //添加到图层
        self.rotationView.layer.addAnimation(animation, forKey: rotationKey)
        
    }
    
    //MARK: - 关闭动画
    func stopRotation() {
      //把带箭头的view显示出来
        arrowRefreshView.hidden = false
      //移除所有核心donghua 
         self.rotationView.layer .removeAllAnimations()
    
    }
    

/*end*/
}
